﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CNVP.JShop.UI;
using CNVP.Framework.Utils;

namespace CNVP.JShop.Web.Member
{
    public partial class OrderShow : UserPage
    {
        BLL.CNVP_Order bll = new BLL.CNVP_Order();
        Model.CNVP_Order model = new Model.CNVP_Order();
        public int id = 0;
        public int t = 103;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]) && Public.IsNumber(Request.QueryString["ID"]))
            {
                id = Convert.ToInt32(Request.QueryString["ID"]);
            }
            if (!IsPostBack)
            {
                DataInfo();
                OrderProListInfo();
            }
        }
        private void DataInfo()
        {
            model = bll.GetSingle_CNVP_OrderById(id);
            if (model != null)
            {
                ViewState["ono"] = model.OrderNum;
                LabTime.Text = model.PostTime.ToString();
                LabZT.Text = BLL.BLLOrder.getorderztClint(model.OrderState);
                string _LevelName = "";
                string _UserName = "";
                int _PointsNum = 0;
                var model_Users=new BLL.CNVP_Users().GetSingle_CNVP_UsersById(model.BuyerID);
                if (model_Users != null)
                {
                    var model_UserLevel = new BLL.CNVP_UserLevel().GetSingle_CNVP_UserLevelById(model_Users.UserLevelID);
                    if (model_UserLevel != null)
                    {
                        _LevelName = model_UserLevel.LevelName;
                    }
                    _UserName = model_Users.UserName;
                    _PointsNum = model_Users.PointsNum;
                }
                LabUser.Text = _LevelName;
                Labprice.Text = model.ProductsTotalAmount.ToString();
                lbDeliveryPrice.Text = model.DeliveryPrice.ToString();
                LabUser.Text = _UserName;
                LabGetJF.Text = _PointsNum.ToString();
                string Address_Str = new BLL.CNVP_Province().GetSingle_CNVP_ProvinceByCode(model.Province).Name + "" + new BLL.CNVP_City().GetSingle_CNVP_CityByCode(model.City).Name;
                Address_Str += new BLL.CNVP_Area().GetSingle_CNVP_AreaByCode(model.Area).Name + model.GetAddress;
                LaAddress.Text = Address_Str;
                string StandardValue_Str = "";
                var OrderProShopMustInfo_List = new BLL.CNVP_OrderProShopMustInfo().GetAll_CNVP_OrderProShopMustInfo(id);
                foreach (Model.CNVP_OrderProShopMustInfo o in OrderProShopMustInfo_List)
                {
                    if (o.StandardValue != "")
                    {
                        StandardValue_Str += o.StandardValue + "；";
                    }
                }
                if (!string.IsNullOrEmpty(StandardValue_Str))
                {
                    LiterMustInfo.Text = "<tr><td colspan=\"2\">购物必填：" + StandardValue_Str + " </td></tr>";
                }
                Labpriall.Text = model.OrderTotalAmount.ToString();
                LabTel.Text = model.Phone;
                LabMob.Text = model.MobilePhone;
                LabZip.Text = model.ZipCode;
                LabName.Text = model.GetTrueName;
                lbDeliveryWay.Text = model.DeliveryWay;
                lbFHno.Text = model.DeliveryNo;
                lbFHnoCompany.Text = model.DeliveryCompany;
                if (model.OrderState == 0 || model.OrderState == 1)
                {
                    //发货单号
                    FH.Visible = false;
                }
                else
                {
                    lbDeliveryWay.Visible = true;
                    //发货单号
                    FH.Visible = true;
                }
                string PayWay_Str = "";
                var model_Column = new BLL.CNVP_Column().GetSingle_CNVP_ColumnById(int.Parse(model.PayWay));
                if (model_Column != null)
                {
                    PayWay_Str = model_Column.ColumnName;
                }
                lbPayWay.Text = PayWay_Str;
                ViewState["PayWay"] = PayWay_Str;
                lbSupportValuePrice.Text = model.SupportValuePrice.ToString();
                lbPayFactorage.Text = model.PayFactorage.ToString();
                lbBilTaxes.Text = model.BilTaxes.ToString();
                lbSalesPromotionAmount.Text = model.SalesPromotionAmount.ToString();
                lbDiscountOrder.Text = model.DiscountOrder.ToString();
                string _Text = model.Text;
                if (!string.IsNullOrEmpty(_Text))
                {
                    LiterText.Text = "<tr><td colspan=\"2\">补充说明：" + _Text + " </td></tr>";
                }
                if (model.IsNeedBill == 1)
                {
                    lbIsNeedBill.Text = "<b>发票抬头</b>";
                }
                MainOrderzt(model.OrderState);
            }
        }

        private void OrderProListInfo()
        {
            model = bll.GetSingle_CNVP_OrderById(id);
            if (model != null)
            {
                string html = "";
                html += "                    <table align=\"center\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" class=\"ListTable\">";
                html += "                        <tr >";
                html += "                            <th nowrap=\"nowrap\" style=\"width:40px;\">";
                html += "                                序号</th>";
                html += "                            <th nowrap=\"nowrap\">";
                html += "                                货号</th>";
                html += "                            <th nowrap=\"nowrap\">";
                html += "                                商品名称</th>";
                html += "                            <th nowrap=\"nowrap\">";
                html += "                                单价</th>";
                html += "                            <th nowrap=\"nowrap\">";
                html += "                                数量</th>";
                html += "                            <th nowrap=\"nowrap\">";
                html += "                                小计</th>";
                html += "                        </tr>";
                string Addtr = "";
                var OrderProducts_List = new BLL.CNVP_OrderProducts().GetAll_CNVP_OrderProducts(id);
                for (int i = 0; i < OrderProducts_List.Count; i++)
                {
                    Addtr += "                        <tr>";
                    Addtr += "                            <td>";
                    Addtr += "                                " + (i + 1) + "</td>";
                    Addtr += "                            <td>";
                    Addtr += "                                " + OrderProducts_List[i].ProductNo + "</td>";
                    Addtr += "                            <td>";
                    Addtr += "                  <a href=\"/ProductDetail_" + OrderProducts_List[i].ProductClassID + "_" + OrderProducts_List[i].ProductID + ".aspx\" target=\"_blank\" title=\"" + OrderProducts_List[i].ProductName + "\">";
                    Addtr += "                                <img align=\"absMiddle\" height=\"60px\" width=\"60px\" class=\"BuyIsPrintPic\" ";
                    Addtr += "                                    src=\"/UploadFile/Product/" + OrderProducts_List[i].ImgProLoad + "\" />";
                    Addtr += "                  </a>";
                    Addtr += "                      <a href=\"/ProductDetail_" + OrderProducts_List[i].ProductClassID + "_" + OrderProducts_List[i].ProductID + ".aspx\" target=\"_blank\" title=\"" + OrderProducts_List[i].ProductName + "\">";
                    Addtr += "                                " + OrderProducts_List[i].ProductName + "";
                    Addtr += "                       </a>";
                    Addtr += "                            </td>";
                    Addtr += "                            <td>";
                    Addtr += "                                ￥" + OrderProducts_List[i].Price + "元</td>";
                    Addtr += "                            <td>";
                    Addtr += "                                " + OrderProducts_List[i].Num + "</td>";
                    Addtr += "                            <td>";
                    Addtr += "                                ￥" + OrderProducts_List[i].TotalPrice + "元</td>";
                    Addtr += "                        </tr>";
                }
                html += Addtr;
                html += "                        <tr class=\"table_data_title\">";
                html += "                            <td colspan=\"6\">";
                html += "                                共计：<font color=\"red\">" + model.ProductsTotalAmount + "</font>￥</td>";
                html += "                        </tr>";
                html += "                    </table>";
                lbOrderProList.Text = html;
            }
        }

        private void MainOrderzt(byte ozt)
        {
            switch (ozt)
            {
                case 0:
                    LabCZ.Text = "您可对该订单做如下操作：";
                    LB2.Visible = false;
                    LabCZ.Text = "您可以 <b><a href='OrderPay.aspx?oid=" + id + "&t="+t+"' style='color:#FF0000'>" + ViewState["PayWay"] + "<font color=blue>立即支付</font></a></b>，";
                    LabMSG.Text = "付款成功的请联系客服。";
                    break;
                case 1:
                    LabCZ.Text = "不可操作！";
                    LB2.Visible = false;
                    LabCZ.Text = "您可以 <b><a href='OrderPay.aspx?oid=" + id + "&t="+t+"' style='color:#FF0000'>" + ViewState["PayWay"] + "<font color=blue>立即支付</font></a></b>，";
                    LabMSG.Text = "";
                    break;
                case 2:
                    LabCZ.Text = "您可对该订单做如下操作：";
                    LB2.Visible = true;
                    LabMSG.Text = "请注意查收您的商品，收到后请点击<确认收货>";
                    LB2.Attributes.Add("onclick", "return confirm('确定收到商品吗?');");
                    break;
                case 3:
                    LabCZ.Text = "";
                    LB2.Visible = false;
                    LabMSG.Text = "对不起，当前您要的商品没有库存。";
                    break;
                case 9:
                    LabCZ.Text = "不可操作！";
                    LB2.Visible = false;
                    LabMSG.Text = "该订单已取消。";
                    break;
                default:
                    LabCZ.Text = "";
                    LB2.Visible = false;
                    LabMSG.Text = "";
                    break;
            }
        }

        protected void LB2_Click(object sender, EventArgs e)
        {
            model.ID = id;
            model.OrderState = 4;
            bll.UpdateOrderState(model);
            MessageUtils.ShowRedirect("确定收货成功！", "OrderList.aspx?olbid=4");
        }
    }
}